package com.renchunlin.loginkeyboard.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.renchunlin.loginkeyboard.R

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/6/21.
 * PS: Not easy to write code, please indicate.
 */
class LoginKeyBoard : LinearLayout, View.OnClickListener {

    private lateinit var onKeyPressListener: OnKeyPressListener

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        //获得布局
        LayoutInflater.from(context).inflate(R.layout.num_key_pad, this)

        init()
    }

    private fun init() {
        this.findViewById<TextView>(R.id.number_1).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_2).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_3).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_4).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_5).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_6).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_7).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_8).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_9).setOnClickListener(this)
        this.findViewById<TextView>(R.id.number_0).setOnClickListener(this)
        this.findViewById<TextView>(R.id.back).setOnClickListener(this)
    }

    override fun onClick(v: View) {
        if (v.id == R.id.back) {
            onKeyPressListener.onBackPress()
        } else {
            val textView = v as TextView
            onKeyPressListener.onNumberPress(textView.text.toString().toInt())
        }
    }

    fun setOnKeyPressListener(onKeyPressListener: OnKeyPressListener) {
        this.onKeyPressListener = onKeyPressListener
    }

    interface OnKeyPressListener {
        fun onNumberPress(number: Int)
        fun onBackPress()
    }
}